package sy.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.AppEquipmentDaoI;
import sy.model.AppEquipment;
import sy.model.AppPlant;
@Repository("appEquipmentDao")
public class AppEquipmentDaoImpl implements AppEquipmentDaoI{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}
	
	@Override
	public Serializable save(AppEquipment appEquipment) {
		return getCurrentSession().save(appEquipment);//直接用
		
	}
	
	@Override
	public void remove(AppEquipment appEquipment) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(appEquipment);
	}
	
	@Override
	public void update(AppEquipment appEquipment) {
		// TODO Auto-generated method stub
		getCurrentSession().update(appEquipment);
	}
	
	@Override
	public List<AppEquipment> getList() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from AppEquipment").list();
	}
	
	@Override
	public AppEquipment getAppEquipmentById(String id) {
		// TODO Auto-generated method stub
		String hql = "from AppEquipment where id = :id";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("id", id);
		List<AppEquipment> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

//	@Override
//	public List<AppEquipment> listAppEquipmentByOwnerId(String ownerId) {
//		// TODO Auto-generated method stub
//		String hql = "select b.id id,b.code code,b.name name,b.category category,b.address address,b.status status,b.price price,b.prod_Date prodDate,b.description description,b.type type"+
//		    " from per_owner_app_equipment_mapping a left join app_equipment b on a.appEquipmentId=b.id where a.owner = :ownerId";
//		SQLQuery query = this.getCurrentSession().createSQLQuery(hql);
//		query.setString("ownerId", ownerId);
//		query.addScalar("id")
//	    .addScalar("code")
//	    .addScalar("name")
//	    .addScalar("category")
//	    .addScalar("address")
//	    .addScalar("status")
//	    .addScalar("price")
//	    .addScalar("prodDate")
//	    .addScalar("description")
//	    .addScalar("type")
//	    .setResultTransformer(Transformers.aliasToBean(AppEquipment.class));
//		List<AppEquipment> list = query.list();
//		return list;
//	}

	@Override
	public AppEquipment getAppEquipmentByCode(String code) {
		// TODO Auto-generated method stub
		String hql = "from AppEquipment where code = :code";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("code", code);
		List<AppEquipment> list = query.list();
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}
	
	
}
